package com.id.supaclean.ui.ram

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import com.id.supaclean.R
import com.id.supaclean.common.BaseFragment
import com.id.supaclean.utils.df2
import com.id.supaclean.utils.getAvailMemory
import kotlinx.coroutines.delay
import kotlin.random.Random

/**
 * Created by YiGo on 2022/6/14.
 */
class MemoryUsageFragment : BaseFragment() {

    //保留两位小数

    override fun layoutId(): Int = R.layout.fragment_memory_usage

    override fun initView() {
        view?.findViewById<ImageView>(R.id.memory_fragment_back_iv)?.setOnClickListener {
            navigate(R.id.action_memoryUsageFragment_to_firstFragment)
        }
        view?.findViewById<ImageView>(R.id.memory_fragment_stop_scan_lav)?.setOnClickListener {
            navigate(R.id.action_memoryUsageFragment_to_firstFragment)
        }
    }

    @SuppressLint("SetTextI18n")
    override fun initEvent() {
        super.initEvent()
        val used = arguments?.getString("used_storage")
        val totalRam = arguments?.getString("all_storage")
        val usedRam = arguments?.getString("used_ram")
        view?.findViewById<TextView>(R.id.ram_num_tv)?.text = used.toString()
        view?.findViewById<TextView>(R.id.ram_used_tv)?.text = "$usedRam GB/ $totalRam"
        lifecycleScope.launchWhenResumed {

            val initialCleanNum = try {
                Random.nextInt(0, getAvailMemory().toInt()) + Random.nextFloat()
            } catch (e: Exception) {
                0.55
            }

            val cleanNum = df2.format(initialCleanNum)
            val bundle = Bundle()
            bundle.apply {
                putString("clean_num", cleanNum.toString())
            }
            delay(3000)
            navigate(R.id.action_memoryUsageFragment_to_ramBoostFragment, bundle)
        }
    }


}